<template>
  <el-dialog
    :title="!dataForm.spec.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <!--      1. 规格名称输入框-->
      {{dataForm}}
      <el-form-item label="规格名称" prop="spec.specName">
        <el-input  v-model="dataForm.spec.specName" placeholder="规格名称"></el-input>
      </el-form-item>
      <!--      2. 添加规格项按钮-->
      <el-button plain type="primary" @click="addSpec()" icon="el-icon-plus">添加规格</el-button>
      <!--      3. 规格项列表-->
      <el-table
        :data="dataForm.options"
        style="width: 100%;">
        <el-table-column
          header-align="center"
          align="center"
          label="规格选项名称">
          <template slot-scope="scope">
            <el-input v-model="scope.row.optionName" placeholder="规格名称"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          label="排序">
          <template slot-scope="scope">
            <el-input v-model="scope.row.orders" placeholder="排序"></el-input>
          </template>
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button type="danger" plain size="small" @click="deleteSpec(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        visible: false,
        dataForm: {
          spec:{
            id:undefined,
            specName: ''
          },
          options:[]
        },
        dataRule: {
          spec:{
            specName: [
              { required: true, message: '名称不能为空', trigger: 'blur' }
            ]
          }
        }
      }
    },
    methods: {
      //1. 添加规格
      addSpec() {
        this.dataForm.options.push({});
      },
      //2.删除规格
      deleteSpec(index) {
        this.dataForm.options.splice(index, 1) //参数1:代表删除改下标属性， 参数2：代表删除元素的个数
      },
      init(id) {
        this.dataForm.spec.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          //this.$refs['dataForm'].resetFields()    //重设表单
          if (this.dataForm.spec.id) {            //修改规格
            this.$http({
              url: this.$http.adornUrl(`/manager/specification/info/${this.dataForm.spec.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                //规格名
                this.dataForm.spec.specName = data.specification.spec.specName;
                //规格项
                this.dataForm.options = data.specification.options;
                console.log("data:",data);
              }
            })
          }else {   //添加，清空表单
            this.dataForm={spec:{},options:[]}
          }
        })
      },
      // 表单提交
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manager/specification/${!this.dataForm.spec.id ? 'save' : 'update'}`),
              method: `${this.dataForm.spec.id ? 'put':'post'}`, //根据表单是否有id值，定义请求方式
              // data: this.$http.adornData({
              //   'id': this.dataForm.spec.id || undefined,
              //   'specName': this.dataForm.spec.specName
              // })
              data: this.dataForm
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
